home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 18 code / Hierarchical Lists / Src / Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-16  |  8.2 KB  |  398 lines  |  [TEXT/KAHL]

  1. /*                                        Main.c                                    */
  2. /*
  3.  * List In A List Sample
  4.  * Main.c
  5.  * Copyright © 1993-94 Apple Computer Inc.
  6.  */
  7. #define EXTERN            /* Allocate variables */
  8. #include "ListInAList.h"
  9. #ifdef __powerc
  10. /*
  11.  * Power PC does not automatically define the QuickDraw globals. This is because
  12.  * only "application" code-fragments need these globals, and this cannot be
  13.  * determined before the code fragment is constructed.
  14.  */
  15. QDGlobals            qd;        /* This is not automatically defined on PowerPC        */
  16. #endif
  17.  
  18. /*
  19.  * These enum's define items in the various menus.
  20.  */
  21. enum AppleMenu {
  22.     kAppleAbout                = 1
  23. };
  24. enum FileMenu {
  25.     kFileQuit                = 1
  26. };
  27. enum EditMenu {
  28.     kEditUndo                = 1,
  29.     kEditUnused,
  30.     kEditCut,
  31.     kEditCopy,
  32.     kEditPaste,
  33.     kEditClear
  34. };
  35.  
  36. Boolean                    gInForeground;
  37.  
  38. /*
  39.  * Local function prototypes.
  40.  */
  41. void                    main(void);
  42. void                    EventLoop(void);
  43. void                    ApplicationEventLoop(void);
  44. void                    DoMouseEvent(void);
  45. void                    ProcessActivateEvent(
  46.         WindowPtr                activeWindow,
  47.         Boolean                    isActivate
  48.     );
  49. void                    DoCommand(
  50.         WindowPtr                activeWindow,
  51.         long                    menuChoice
  52.     );
  53. void                    AdjustMenus(void);
  54. void                    InitMacintosh(void);
  55. void                    InitApplication(void);
  56. void                    AdjustEditMenu(
  57.         Boolean                    isDeskAcc
  58.     );
  59. static Boolean            IsOurWindow(
  60.         WindowPtr                theWindow
  61.     );
  62.  
  63. /*
  64.  * main
  65.  * The application main program. This is a limited program for the twist-down
  66.  * sample; it is not intended as a model for a complete Macintosh program.
  67.  */
  68. void
  69. main()
  70. {
  71.         InitMacintosh();
  72.         InitApplication();
  73.         gInForeground = TRUE;
  74.         MakeHFSBrowserWindow();
  75.         InitCursor();
  76.         while (gQuitNow == FALSE)
  77.             EventLoop();
  78.         ExitToShell();
  79. }
  80.  
  81. /*
  82.  * Application event loop: process one event each time.
  83.  */    
  84. void
  85. EventLoop(void)
  86. {
  87.         long                            menuChoice;
  88.         register WindowPtr                theWindow;
  89.         GrafPtr                            savePort;
  90.         Boolean                            isActivating;
  91.         
  92.         if (gUpdateMenusNeeded)
  93.             AdjustMenus();
  94.         WaitNextEvent(
  95.             everyEvent,
  96.             &gEventRecord,
  97.             (gInForeground) ? 10L : 60L,
  98.             NULL
  99.         );
  100.         theWindow = FrontWindow();
  101.         switch (gEventRecord.what) {
  102.         case nullEvent:
  103.             break;
  104.         case keyDown:
  105.         case autoKey:
  106.             if ((gEventRecord.message & charCodeMask) == '.'
  107.              && (gEventRecord.modifiers & cmdKey) != 0) {
  108.                 FlushEvents(keyDown | autoKey, 0);
  109.                 gQuitNow = TRUE;
  110.             }
  111.             else if ((gEventRecord.modifiers & cmdKey) != 0) {
  112.                 if (gEventRecord.what == keyDown) {
  113.                     menuChoice = MenuKey(gEventRecord.message & charCodeMask);
  114.                     if (HiWord(menuChoice) != 0 && IsOurWindow(theWindow))
  115.                         DoCommand(theWindow, menuChoice);
  116.                     else if (IsOurWindow(theWindow))
  117.                         DoWindowKeyDown((BrowserPtr) theWindow);
  118.                     else {
  119.                         SysBeep(10);
  120.                     }
  121.                 }
  122.             }
  123.             else if (IsOurWindow(theWindow))
  124.                 DoWindowKeyDown((BrowserPtr) theWindow);
  125.             else {
  126.                 SysBeep(10);
  127.             }
  128.             break;
  129.         case mouseDown:
  130.             DoMouseEvent();
  131.             break;
  132.         case updateEvt:
  133.             theWindow = (WindowPtr) gEventRecord.message;
  134.             GetPort(&savePort);
  135.             SetPort(theWindow);
  136.             BeginUpdate(theWindow);
  137.             EraseRect(&theWindow->portRect);
  138.             if (IsOurWindow(theWindow) == FALSE)
  139.                 DrawControls(theWindow);
  140.             else {
  141.                 UpdateBrowserWindow((BrowserPtr) theWindow, theWindow->visRgn);
  142.             }
  143.             EndUpdate(theWindow);
  144.             SetPort(savePort);
  145.             break;
  146.         case activateEvt:
  147.             theWindow = (WindowPtr) gEventRecord.message;
  148.             isActivating = ((gEventRecord.modifiers & activeFlag) != 0);
  149.             goto activateEvent;
  150.             break;
  151.         case osEvt:
  152.             switch (((unsigned long) gEventRecord.message) >> 24) {
  153.             case mouseMovedMessage:
  154.                 break;
  155.             case suspendResumeMessage:
  156.                 isActivating = ((gEventRecord.message & 0x01) != 0);
  157. activateEvent:        if (isActivating) {
  158.                     /*
  159.                      * Activate this window. Activate events define theWindow from
  160.                      * the event record, while suspend/resume uses the pre-set
  161.                      * FrontWindow value.
  162.                      */
  163.                     SelectWindow(theWindow);
  164.                     (void) TEFromScrap();
  165.                 }
  166.                 if (IsOurWindow(theWindow)) {
  167.                     ActivateBrowser(
  168.                         (BrowserPtr) theWindow,
  169.                         isActivating
  170.                     );
  171.                     /*
  172.                      * Globalize the current window for the debugger's convenience.
  173.                      */
  174.                     if (isActivating)
  175.                         gCurrentBrowserPtr = (BrowserPtr) theWindow;
  176.                 }
  177.                 else {
  178.                     /* Desk accessory or what? */
  179.                 }
  180.                 gInForeground = isActivating;
  181.                 gUpdateMenusNeeded = TRUE;
  182.                 break;
  183.             }
  184.             break;
  185.         }
  186. }
  187.  
  188. /*
  189.  * DoMouseEvent
  190.  * The user clicked on something. Handle application-wide processing here, or call
  191.  * a Browser function for specific action.
  192.  */
  193. void
  194. DoMouseEvent(void)
  195. {
  196.         WindowPtr        theWindow;
  197.         short            whichPart;
  198.         
  199.         whichPart = FindWindow(gEventRecord.where, &theWindow);
  200.         if (theWindow == NULL)
  201.             theWindow = FrontWindow();
  202.         if (whichPart == inMenuBar && IsOurWindow(theWindow) == FALSE)
  203.             theWindow = FrontWindow();
  204.         switch (whichPart) {
  205.         case inDesk:
  206.             break;
  207.         case inMenuBar:
  208.             InitCursor();
  209.             DoCommand(theWindow, MenuSelect(gEventRecord.where));
  210.             break;
  211.         case inDrag:
  212.             DragWindow(theWindow, gEventRecord.where, &qd.screenBits.bounds);
  213.             break;
  214.         case inGoAway:
  215.             if (TrackGoAway(theWindow, gEventRecord.where)) {
  216.                 if (IsOurWindow(theWindow)) {
  217.                     DisposeBrowser((BrowserPtr) theWindow);
  218.                     if (gOpenWindowCount <= 0)
  219.                         gQuitNow = TRUE;
  220.                 }
  221.                 else {
  222.                     SysBeep(10);
  223.                 }
  224.             }
  225.             break;
  226.         case inContent:
  227.             if (theWindow != FrontWindow())
  228.                 SelectWindow(theWindow);
  229.             else if (IsOurWindow(theWindow)) {
  230.                 DoContentClick((BrowserPtr) theWindow);
  231.             }
  232.             else {
  233.                 /* Nothing happens here        */
  234.             }
  235.             break;
  236.         default:
  237.             break;                            /* Bogus click: ignore                */
  238.         }
  239. }
  240.  
  241. /*
  242.  * DoCommand
  243.  * Process a menu or keystroke command.
  244.  */
  245. void
  246. DoCommand(
  247.         WindowPtr                theWindow,
  248.         long                    menuChoice
  249.     )
  250. {
  251.         short                menuItem;
  252.         Str255                menuText;
  253.         GrafPtr                savePort;
  254.         
  255.         menuItem = LoWord(menuChoice);
  256.         switch (HiWord(menuChoice)) {
  257.         case MENU_Apple:
  258.             if (menuItem == kAppleAbout)
  259.                 ; /* Nothing happens here */
  260.             else {
  261.                 GetItem(gAppleMenu, menuItem, menuText);
  262.                 AdjustEditMenu(TRUE);
  263.                 GetPort(&savePort);
  264.                 OpenDeskAcc(menuText);
  265.                 SetPort(savePort);
  266.                 AdjustEditMenu(IsOurWindow(theWindow) == FALSE);
  267.             }
  268.             break;
  269.         case MENU_File:
  270.             switch (menuItem) {
  271.             case kFileQuit:
  272.                 gQuitNow = TRUE;
  273.                 break;
  274.             }
  275.             break;
  276.         case MENU_Edit:
  277.             if (SystemEdit(menuItem - 1) == FALSE)
  278.                 SysBeep(10);
  279.             break;
  280.         }
  281.         HiliteMenu(0);
  282. }        
  283.  
  284. /*
  285.  * AdjustMenus
  286.  * Enable/disable menu options.
  287.  */
  288. void
  289. AdjustMenus(void)
  290. {
  291.         EnableItem(gFileMenu, kFileQuit);
  292.         AdjustEditMenu(IsOurWindow(FrontWindow()) == FALSE);
  293. }
  294.  
  295. /*
  296.  * AdjustEditMenu
  297.  * Enable/disable Edit Menu options.
  298.  */
  299. void
  300. AdjustEditMenu(
  301.         Boolean                isDeskAcc
  302.     )
  303. {
  304.         if (isDeskAcc) {
  305.             EnableItem(gEditMenu, kEditUndo);
  306.             EnableItem(gEditMenu, kEditCut);
  307.             EnableItem(gEditMenu, kEditCopy);
  308.             EnableItem(gEditMenu, kEditPaste);
  309.             EnableItem(gEditMenu, kEditClear);
  310.         }
  311.         else {
  312.             DisableItem(gEditMenu, kEditUndo);
  313.             DisableItem(gEditMenu, kEditCut);
  314.             DisableItem(gEditMenu, kEditCopy);
  315.             DisableItem(gEditMenu, kEditPaste);
  316.             DisableItem(gEditMenu, kEditClear);
  317.         }
  318. }
  319.  
  320. /*
  321.  * InitMacintosh
  322.  * Perform the normal application initialization. This must be extended for "real"
  323.  * applications. The only thing this module does is initialize the managers.
  324.  */
  325. void
  326. InitMacintosh(void)
  327. {
  328.         int                i;
  329.         
  330.         MaxApplZone();        
  331.         InitGraf(&qd.thePort);
  332.         InitFonts();
  333.         InitWindows();
  334.         InitMenus();
  335.         TEInit();
  336.         InitDialogs(NULL);
  337.         HNoPurge((Handle) GetCursor(watchCursor));
  338.         SetCursor(*GetCursor(watchCursor));
  339.         for (i = 0; i < 3; i++)
  340.             EventAvail(everyEvent, &gEventRecord);
  341. }
  342.  
  343. /*
  344.  * InitApplication
  345.  * Continue initialization.
  346.  */
  347. void
  348. InitApplication(void)
  349. {
  350.         (void) TEFromScrap();
  351.         SetMenuBar(GetNewMBar(MBAR_MenuBar));
  352.         gAppleMenu = GetMHandle(MENU_Apple);
  353.         gFileMenu = GetMHandle(MENU_File);
  354.         gEditMenu = GetMHandle(MENU_Edit);
  355.         AddResMenu(GetMHandle(MENU_Apple), 'DRVR');
  356.         DrawMenuBar();
  357.         SetupAnimatedCursor(ACUR_Animator);
  358. }
  359.  
  360. /*
  361.  * IsOurWindow
  362.  * Return TRUE if this is a browser window.
  363.  */
  364. Boolean
  365. IsOurWindow(
  366.         WindowPtr            theWindow
  367.     )
  368. {
  369.         if (theWindow == NULL
  370.          || ((WindowPeek) theWindow)->windowKind != userKind)
  371.             return (FALSE);
  372.         else {
  373.             return (TRUE);
  374.         }
  375. }
  376.  
  377. void pstrcpy(
  378.         StringPtr                dst,
  379.         StringPtr                src
  380.     )
  381. {
  382.         BlockMove(src, dst, src[0] + 1);
  383. }
  384.  
  385. void pstrcat(
  386.         StringPtr                dst,
  387.         StringPtr                src
  388.     )
  389. {
  390.         short                    length;
  391.         
  392.         length = 255 - dst[0];
  393.         if (length > src[0])
  394.             length = src[0];
  395.         BlockMove(&src[1], &dst[1] + dst[0], length);
  396.         dst[0] += length;
  397. }
  398.